package common;


import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.util.Map;

public abstract class SimpleXMLParser
{
	public Map<String, Object> IdMap;
	protected SAXParser saxParser;
	protected DefaultHandler saxHandler;

	protected SimpleXMLParser(DefaultHandler handler)
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try
		{
			saxParser = factory.newSAXParser();
		} catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Error in getting SAX Parser");
			System.exit(-1);
		}
		this.saxHandler = handler;
	}

	@Deprecated
	public void parse(String file_path)
	{
		File file = new File(file_path);
		try
		{
			saxParser.parse(file, saxHandler);
		} catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Error in parsing file: " + file_path);
			System.exit(-1);
		}
	}

	public void parse(File file)
	{
		try
		{
			saxParser.parse(file, saxHandler);
		} catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Error in parsing file: " + file.getPath());
			System.exit(-1);
		}
	}
}